(function(KKBus){

    function AuthenticationController() {
        var self = this;
        
        self.user;
                
        self.authenticated = false;
        self.newUser=new KKBus.UserModel("","","");

        self.loadAuthenticatedUser = function() {  
            app.request++;
            KKBus.Ajax.doGet('rest/user/authenticated', function(data, textStatus, jqXHR) {
                var jsData = JSON.parse(data);
                self.user = new KKBus.UserModel(jsData.id, jsData.name, jsData.surname, jsData.email, jsData.phone, jsData.password, jsData.birthday, jsData.points);
                
                var i = jsData.authorities.length;
                while(i--) {
                    self.user.roles.push(jsData.authorities[i]);
                }
                self.authenticated = true;
                app.request--;
                
                if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + AuthenticationController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
        
        self.loadAnonymousUser = function() {
            if (typeof self.user === "undefined") {
                self.user = new KKBus.UserModel(0, "Gość", "", "");
            }
        };
        
        self.hasRole = function(role) {
            if (self.user.roles.indexOf(role) != -1) {
                return true;
            } else {
                return false;
            }
        }
        
        self.activated = ko.observable(false);        
        self.activateUser = function(hash) {
            self.activated(false);
            app.request++;
            KKBus.Ajax.doGet('rest/user/activate/' + hash, function(data, textStatus, jqXHR) {
                var jsData = JSON.parse(data);
                
                if(jsData == "SUCCESS") {
                    self.activated(true);
                }
                
                app.request--;
                
                if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + AuthenticationController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        }
        
        self.validation = ko.observable(false);
        self.errorMessage = ko.observable("Lorem ipsum dolor sit amet!");
        self.wrongName = ko.observable(true);
        self.success = ko.observable(false);      



        self.registerUser = function() {
        
            var user = JSON.stringify({
                    name: $('#formImie').val(),
                    surname: $('#formNazwisko').val(),
                    birthday: $('#formRok').val() + "-" + $('#formMiesiac').val() + "-" + $('#formDzien').val(),
                    email: $('#formEmail').val(),
                    phone: $('#formTel').val(),
                    password: $('#formHaslo').val()
                    
                });
        
            if(KKBus.Validation.length($('#formImie').val())
                && KKBus.Validation.length($('#formNazwisko').val())
                && KKBus.Validation.length($('#formDzien').val())
                && KKBus.Validation.length($('#formRok').val())
                && KKBus.Validation.length($('#formEmail').val())
                && KKBus.Validation.length($('#formTel').val())
                && KKBus.Validation.length($('#formHaslo').val())){
            
                if(KKBus.Validation.dz($('#formDzien').val())) {
                  if(KKBus.Validation.rok($('#formRok').val())) {
            
                    if(KKBus.Validation.word($('#formImie').val())
                        && KKBus.Validation.word($('#formNazwisko').val()))    
                        {
                            
                         if(KKBus.Validation.em($('#formEmail').val()))
                           {
                             if(KKBus.Validation.pas($('#formHaslo').val())){ 
                                 
                                 if(KKBus.Validation.tel($('#formTel').val())){
                                                   
                                    KKBus.Ajax.doPost('rest/user', user, function(result, textStatus, jqXHR) {
                                      if (result == "ADDED") {
                                         self.success(true);
                                         setTimeout(function() { 
                                         window.location = "login";
                                         }, 3000);
                                        }
                                    });
                                } else {
                                   self.validation(true);
                                   self.errorMessage("Błędnie wprowadzony numer!");
                                   setTimeout(function() { self.validation(false); }, 5000);
                                 
                               };
                               
                             }else {
                                self.validation(true);
                                self.errorMessage("Zbyt krotkie haslo!");
                                setTimeout(function() { self.validation(false); }, 5000);                                  
                             };
                             
                           }else {
                              
                                self.validation(true);
                                self.errorMessage("Wprowadziles błędny email!");
                                setTimeout(function() { self.validation(false); }, 5000); 
                               
                           };
                            
                       } else {
                
                           self.validation(true);
                           self.errorMessage("Niepoprawne znaki w polu imie lub nazwisko!");
                           setTimeout(function() { self.validation(false); }, 5000);
                    };
                    
                  }else {
                     self.validation(true);
                     self.errorMessage("Bledny rok!");
                     setTimeout(function() { self.validation(false); }, 5000); 
                  };
                  
                } else {
                  
                  self.validation(true);
                  self.errorMessage("Bledna liczba dni!");
                  setTimeout(function() { self.validation(false); }, 5000);
                       
                    
                };
                    
                                    
               
          } else {
                
                self.validation(true);
                self.errorMessage("Puste pola!");
                setTimeout(function() { self.validation(false); }, 5000);
            } 
            
             
    };

    self.sent = ko.observable(false);  
    self.sentError = ko.observable(false);    
    self.sendEmail = function() {
        self.sent(false);
        self.sentError(false);
        
        if(KKBus.Validation.length($('#formEmail2').val())
           && KKBus.Validation.length($('#formTemat').val())
           && KKBus.Validation.length($('#formTresc').val())){
       
           if(KKBus.Validation.word($('#formTemat').val())) {
               
               if(KKBus.Validation.em($('#formEmail2').val())){
              
                    var emailMessage = JSON.stringify({
                        to: $('#formEmail2').val(),
                        subject: $('#formTemat').val() + ' - ' + $('#formKategoria').val(),
                        message: $('#formTresc').val()
                    });
                
                    KKBus.Ajax.doPost('rest/contact/message', emailMessage, function(result, textStatus, jqXHR) {
                        if (result == "SENT") {
                            self.sent(true);
                            setTimeout(function() { 
                                self.sent(false);
                            }, 5000);
                        } else {
                            self.sentError(true);
                            setTimeout(function() { 
                                self.sentError(false);
                            }, 5000);
                        }
                    });
                    
               }else{
                 self.validation(true);
                 self.errorMessage("Wprowadziles błędny email!");
                 setTimeout(function() { self.validation(false); }, 5000); 
                   
               }
            
             }else{
                  
               self.validation(true);
               self.errorMessage("Niepoprawne znaki w temacie!");
               setTimeout(function() { self.validation(false); }, 5000);   
                 
              }
       
       
         }else{
               
               
          self.validation(true);
          self.errorMessage("Puste pola!");
          setTimeout(function() { self.validation(false); }, 5000);
               
        }
           
    };
       
    
   
   self.editName = ko.observable(false);
   self.editSurname = ko.observable(false);
   self.editPhone = ko.observable(false);
   self.editEmail = ko.observable(false);
   self.editPass = ko.observable(false);

     
   
    
    self.enableInput = function() {
        self.editName(true);
                        
    };
    
    self.enableInput2 = function() {
        self.editSurname(true);
        
    };
    
    self.enableInput3 = function() {
        self.editPhone(true);
    };
    
    self.enableInput4 = function() {
        self.editEmail(true);
    };
    
    self.enableInput5 = function() {
        self.editPass(true);
    };
    
    self.saveChanges = function() {
        self.editPass(false);
        self.success(false);
        self.validation(false);
      
       
                
         if(KKBus.Validation.length($('#formImie').val())
                && KKBus.Validation.length($('#formNazwisko').val())
                   && KKBus.Validation.length($('#formEmail').val()) 
                     && KKBus.Validation.length($('#formTel').val())
                       ){
               if(KKBus.Validation.em($('#formEmail').val())){
                   if(KKBus.Validation.tel($('#formTel').val())){            
                      if(KKBus.Validation.word($('#formImie').val())
                        && KKBus.Validation.word($('#formNazwisko').val())){ 
                          
                        var user = app.Knockout.AuthenticationController.user;
                        var userId = user.id();
                        //var pass = user.email();
                        var userDate = user.birthday()*1000;
                        
                          var userSave = JSON.stringify({
                                                             
                              id: userId, 
                              name: $('#formImie').val(),
                              surname: $('#formNazwisko').val(),
                              phone: $('#formTel').val(),
                              email: $('#formEmail').val(), 
                              password: 'qwerty123',
                              birthday: userDate
                              });
                              
                          KKBus.Ajax.doPut('rest/user/'+ app.Knockout.AuthenticationController.user.id(), userSave, function(result, textStatus, jqXHR) {
                                      if (result == "ADDED") {
                                         self.success(true);
                                         console.log(userId);
                                         setTimeout(function() { 
                                           self.success(false);
                                         }, 3000);
                                        }
                                    });    
                                 
                                                
                        
                        }else{
                        
                        self.validation(true);
                        self.errorMessage("Niepoprawne znaki w polu imie lub nazwisko!");
                        setTimeout(function() { self.validation(false); }, 5000);
                        
                        }
                                    
                   }else{
                    
                    self.validation(true);
                    self.errorMessage("Błędnie wprowadzony numer!");
                    setTimeout(function() { self.validation(false); }, 5000);
                    
                   }
           
                }else{
                   
                self.validation(true);
                self.errorMessage("Wprowadziles błędny email!");
                setTimeout(function() { self.validation(false); }, 5000); 
                
               };
           
             }else{
                    
           self.validation(true);
           self.errorMessage("Puste pola!");
           setTimeout(function() { self.validation(false); }, 5000);
        }
                    
         
        
          
      }; 
        
 self.wiad = ko.observable(false);
    
    self.wyswietlUsterki = function() {
      
         
            self.wiad(true); 
            
             
    };
    
    self.deleteUser = function(id) {        
        KKBus.Ajax.doDelete('rest/user/'+id, function(result, textStatus, jqXHR) {
    });   
    };    

      self.banUser = function(id) {
         KKBus.Ajax.doPut('rest/user/ban/'+id, function(result, textStatus, jqXHR) {
    });   
    };    
    
       self.unbanUser = function(id) {
         KKBus.Ajax.doPut('rest/user/unban/'+id, function(result, textStatus, jqXHR) {
    });   
    };        };

    KKBus.AuthenticationController = AuthenticationController;
    
}(window.KKBus));